package com.ziyun.pop.rest.module.order;

import com.service.pay.conf.weixinpaySdk.HttpXmlUtils;
import com.ziyun.pop.modules.order.dto.TwbOrderListDto;
import com.ziyun.pop.modules.order.service.ErpOrderService;
import io.renren.common.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * @Author llc
 * @Description
 * @Date 2019/2/25 11:28
 */
@Api(value = "OrderRestApi", tags = "门店订单支付")
@RestController
@RequestMapping("port/order/")
public class OrderController {

    @Autowired
    private ErpOrderService erpOrderService;

    @ApiOperation(value = "订单信息")
    @ApiResponses(value = @ApiResponse(code =200, message = "查询订单信息成功",response =R.class))
    @RequestMapping(value="/getErpOrderInfo/{orderNo}", method= RequestMethod.GET)
    @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, dataType = "String", paramType = "path",example= "314190225142180")
    public R getErpOrderInfo(@PathVariable String orderNo){
        return erpOrderService.getErpOrderInfoForPay(orderNo);
    }

    @ApiOperation(value = "获取订单支付信息")
    @ApiResponses(value = @ApiResponse(code =200, message = "查询获取订单支付信息成功",response =R.class))
    @RequestMapping(value="/getOrderPayInfo/{orderNos}", method= RequestMethod.GET)
    public R getOrderPayInfo(@PathVariable("orderNos") String orderNos){
        return erpOrderService.getOrderPayInfo(orderNos);
    }


    @ApiOperation(value = "开始微信支付")
    @ApiResponses(value = @ApiResponse(code =200, message = "返回微信支付信息成功",response =R.class))
    @RequestMapping(value="/beginOrderPay", method= RequestMethod.POST)
    public R beginOrderPay(@RequestBody Map<String,Object> map){
        return erpOrderService.beginOrderPay(map);
    }

    @RequestMapping(value="/orderPayWeixinCallBack", method= RequestMethod.POST)
    public void orderPayWeixinCallBack(HttpServletRequest request, HttpServletResponse response){
      Boolean  flag=  erpOrderService.orderPayWeixinCallBack(request);
        try {
            //返回给微信结果
            if (flag) {
                PrintWriter writer = response.getWriter();
                writer.write(HttpXmlUtils.backWeixin("SUCCESS", "OK"));
                if (writer != null) {
                    writer.flush();
                    writer.close();
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
